package com.ns.attendance.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//邮件，log4j,加密
public class EmailSend {
	/**
	 * 发送text消息
	 * @param message
	 * @throws MessagingException 
	 */
	public static void sendTextMsg(String title,String msg,String email) throws Exception{
		Properties props = new Properties();
		Transport transport;
		Session sendMailSession;
		sendMailSession  = Session.getInstance(props,new Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(LoadConfig.getValue("mailadd"),LoadConfig.getValue("mailpass"));
			}
		});
		props.put("mail.smtp.host", LoadConfig.getValue("mailhost"));
		props.put("mail.smtp.port", LoadConfig.getValue("mailport"));
		props.put("mail.smtp.auth",LoadConfig.getValue("mailauth"));
		
		Message message = new MimeMessage(sendMailSession);
		message.setFrom(new InternetAddress(LoadConfig.getValue("mailadd")));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
		message.setSubject(title);
		message.setSentDate(new Date());
		message.setText(msg);
		
		transport = sendMailSession.getTransport(LoadConfig.getValue("mailtransport"));
		transport.send(message);
	}
}
